//
//  RevisingPersonalDataViewController.m
//  yunyan
//
//  Created by haichuang on 2018/3/27.
//  Copyright © 2018年 haichuang. All rights reserved.
//

#import "RevisingPersonalDataViewController.h"
#import "ChangeHeaderImageCell.h"
#import "ChangeUserInformationCell.h"
#import<Photos/Photos.h>
#import "RegisterViewController.h"
@interface RevisingPersonalDataViewController ()<UITableViewDelegate, UITableViewDataSource,UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate>
@property (nonatomic, strong) UITableView *personTab;
@property (nonatomic, strong) NSArray * titleArr;
@property (nonatomic, strong) NSArray * dataArr;
@property (nonatomic, strong) UIImagePickerController * imagePickerController;
@property (nonatomic, strong) UIImage *selectImage;;
@end

@implementation RevisingPersonalDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNaviItemTitle:@"修改个人资料" isLeft:YES];
    [self customTabBarButtonTitle:@"完成" image:nil target:self action:@selector(rightButtonEvent) isLeft:NO];
    self.personTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    [self.view addSubview:self.personTab];
    
    WeakSelf;
    [_personTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.top.equalTo (weakSelf.view);
        
    }];
    
    
    self.personTab.delegate = self;
    self.personTab.dataSource = self;
    

    [self.personTab registerClass:[ChangeHeaderImageCell class ] forCellReuseIdentifier:@"ChangeHeaderImageCell"];
     [self.personTab registerClass:[ChangeUserInformationCell class ] forCellReuseIdentifier:@"ChangeUserInformationCell"];
    
    _titleArr = @[@"昵称",@"性别",@"手机号",@"生日"];
    _dataArr = @[@"千与千寻",@"男",@"13569874429",@"1999-05-09"];
}
-(void)rightButtonEvent{
    
    [UIUtils showHudSuccessWithTint:@"修改成功"];
}


#pragma mark - tableview delegete;


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0) {
        
        return 1;
    }else{
        
        return 4;
    }
    
   
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return 72;
    }else{
        
        return 50;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    if (section == 0) {
        
        return 16;
    }else{
        
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return nil;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return CGFLOAT_MIN;

}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        ChangeHeaderImageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeHeaderImageCell" forIndexPath:indexPath];
          cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.image.image = _selectImage;
        return cell;
    }else{
    
    ChangeUserInformationCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeUserInformationCell" forIndexPath:indexPath];
    
        if (indexPath.row == 2) {
            cell.userTF.enabled = NO;
        }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   
    cell.nameLabel.text = _titleArr[indexPath.row ];
    cell.userTF.text = _dataArr[indexPath.row];

    return  cell;
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        [self ChangeUserImage];
        
    }else if (indexPath.section == 1)
    
    {
        if (indexPath.row == 2) {
            
            RegisterViewController * registerVC = [[RegisterViewController alloc] init];
            [self.navigationController pushViewController:registerVC animated:YES];
        }
        
    }
   
    
}

-(void)ChangeUserImage{
  
    [LSActionSheet showWithTitle:nil destructiveTitle:nil otherTitles:@[@"拍照",@"从相册选择"] block:^(int index) {
        if (index == 0) {
            
            //相机权限
            AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
            
            if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。
                
                authStatus ==AVAuthorizationStatusDenied)  //用户已经明确否认了这一照片数据的应用程序访问
            {
                
              //  [UIUtils showHudFailWithTint:@"请在“设置－隐私－相机”选项中，允许访问您的相机。"];
            }else{
                
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                    _imagePickerController = [[UIImagePickerController alloc] init];
                    _imagePickerController.delegate = self;
                    //设置拍照后的图片可被编辑
                    _imagePickerController.allowsEditing = YES;
                    _imagePickerController.sourceType =     UIImagePickerControllerSourceTypeCamera;
                    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                    _imagePickerController.mediaTypes = @[mediaTypes[0],mediaTypes[1]];
                    [self presentViewController:_imagePickerController animated:YES completion:nil];
                }
                
            }
        }if (index == 1) {
            
            PHAuthorizationStatus libraryStatus = [PHPhotoLibrary authorizationStatus];
            if (libraryStatus == PHAuthorizationStatusDenied || libraryStatus == PHAuthorizationStatusRestricted) {
                
             //   [UIUtils showHudFailWithTint:@"请在“设置－隐私－照片”选项中，允许访问您的照片。"];
                
            }
            else {
                //                [weakSelf presentViewController:alert animated:YES completion:nil];
                if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                    // 配置图片选取器
                    // 初始化图片选取器
                    _imagePickerController = [[UIImagePickerController alloc] init];
                    // 配置代理
                    _imagePickerController.delegate = self;
                    _imagePickerController.allowsEditing = YES;
                    
                    // 获取支持媒体格式
                    NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
                    
                    
                    
                    // 配置sourceType
                    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    // 配置支持的媒体格式
                    _imagePickerController.mediaTypes = @[mediaTypes[0],mediaTypes[1]];
                    // 配置是否允许编辑图片
                    _imagePickerController.allowsEditing = YES;
                    
                }
                [self presentViewController:_imagePickerController animated:YES completion:nil];
            }
            
        }
        
    }];
    
    
    
}
#pragma mark - UIImagePickerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    _selectImage = image;
    [picker dismissViewControllerAnimated:YES completion:NULL];
     [self.personTab reloadData];
}






@end
